﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.fmtx.Model;
using com.fmtx.IDal;
using fmtx.Framework.NotSqlHelper;
using fmtx.Framework.Expend;

namespace com.fmtx.Dal
{
    public class PlaceServer:BaseServer<Place>,IPlace
    {
        public bool IsExists(string name)
        {
            var model=db.AllList(w=>w.Name==name).FirstOrDefault();
            return model != null && model.Id > 0;
        }


        public List<Place> QueryByName(string name,int cityid, int num)
        {
            var list = db.AllList();
            if (cityid != 0)
            {
                list=list.Where(w => w.CityId == cityid);
            }
            list = list.Where(w => w.Name.Contains(name) || w.EnName.Contains(name) || w.PingYin.Contains(name));
            return list.Take(num).ToList();
        }


        public IList<Place> Query(int index, int size, string name, int cityid, out int count)
        {
            var list = db.AllList();
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(w => w.Name.Contains(name));
            }
            if (cityid > 0)
            {
                list = list.Where(w => w.CityId == cityid);
            }
            count = list.Count();
            return list.OrderBy(o => o.Id).Skip(index * size).Take(size).ToList();
        }



        public IList<Place> QueryIdAndName()
        {
            
            //MongoOperation<Place1> p1 = new MongoOperation<Place1>();
            //var list = p1.AllList();
            //foreach (var item in list)
            //{
            //    Place pb = new Place();
            //    pb.CopyModel(item);
            //    db.Add(pb);
            //}

            //CityServer sb = new CityServer();
            //MongoOperation<City1> c1 = new MongoOperation<City1>();
            //var cblist = c1.AllList();
            //foreach (var item in cblist)
            //{
            //    City pb = new City();
            //    pb.CopyModel(item);
            //    sb.Add(pb);
            //}

            //CountryServer ccb = new CountryServer();
            
            //MongoOperation<Country1> cc1 = new MongoOperation<Country1>();
            //var ccblist = cc1.AllList();
            //foreach (var item in ccblist)
            //{
            //    Country pb = new Country();
            //    pb.CopyModel(item);
            //    ccb.Add(pb);
            //}

            var query = from q in db.AllList()
                        select new Place
                        {
                            Id = q.Id,
                            Name = q.Name
                        };
            return query.ToList();
                
        }
    }
}
